package com.jt.blog.controller;

import com.jt.blog.common.model.Response;
import com.jt.blog.common.model.TreeNode;
import com.jt.blog.dto.CommentDto;
import com.jt.blog.model.Blog;
import com.jt.blog.model.BlogStatistics;
import com.jt.blog.model.Comment;
import com.jt.blog.model.User;
import com.jt.blog.service.BlogReadService;
import com.jt.blog.service.BlogStatisticsService;
import com.jt.blog.service.CommentReadService;
import com.jt.blog.service.CommentWriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : 戴瑞
 * @Description :
 * @create : 2018-03-31 21:30
 **/

@Controller
public class CommentController {

    @Autowired
    private BlogReadService blogReadService;

    @Autowired
    private BlogStatisticsService blogStatisticsService;

    @Autowired
    private CommentReadService commentReadService;

    @Autowired
    private CommentWriteService commentWriteService;

    @RequestMapping("comments/{blogId}")
    @ResponseBody
    public Object comments(@PathVariable Long blogId) {
        TreeNode<CommentDto> root = commentReadService.get(blogId, 1);
        return root;
    }

    @RequestMapping("comment/reply")
    public String reply(Model model, HttpSession session, Comment comment) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "front/login";
        } else {
            model.addAttribute("comment", comment);
            return "front/comment";
        }
    }

    @RequestMapping("comment/create")
    @ResponseBody
    public Object comment(HttpSession session, Comment comment) {
        User user = (User) session.getAttribute("user");
        Map<String, Object> map = new HashMap<String, Object>();
        if (user == null) {
            map.put("flag", false);
            map.put("errMsg", "please login first");
        } else {
            comment.setReplyerId(user.getId());
            commentWriteService.create(comment);
            BlogStatistics blogStatistics = blogStatisticsService.get(comment.getBlogId());
            blogStatistics.setCommentCount(blogStatistics.getCommentCount() + 1);
            blogStatisticsService.update(blogStatistics);
            map.put("flag", true);
        }
        return map;
    }

    @RequestMapping("comment/delete")
    @ResponseBody
    public Object deleteComment(HttpSession session, Comment comment) {
        User user = (User) session.getAttribute("user");
        Map<String, Object> map = new HashMap<>();
        Blog blog = blogReadService.get(comment.getBlogId()).getResult();
        // 如果当前博客属于当前用户。或者该评论属于当前用户，就可以进行删除操作
        if (comment.getReplyerId().equals(user.getId())||blog.getUserId().equals(user.getId())){
            commentWriteService.delete(comment);
            map.put("flag",true);
        }else {
            map.put("flag", false);
            map.put("errMsg", "没有权限");
        }
        return map;
    }

}
